package switchviz.recording;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import org.jdom.JDOMException;

import switchviz.core.ResourceManager;
import switchviz.core.alias.AliasTree;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.PaddingPanel;
import switchviz.gui.component.ToggleSelectionModel;

public class RecordCreationPanel extends PaddingPanel implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8369141176321703886L;
	private JButton _buttselectall = null;
	private JButton _buttselectnone = null;
	private JLabeldComponent _txtfile = null;
	private JList _aliaslist = null;
	
	public String getRecordName()
	{
		return _txtfile.getText();
	}
	
	public RecordCreationPanel() throws Exception
	{
		initialize();
	}
	
	private void initialize() throws JDOMException, IOException
	{
		_txtfile = new JLabeldComponent("Record Name: ", JLabeldComponent.TEXTFIELD);
		_txtfile.setText("New Record");
		_buttselectall = new JButton("Select All");
		_buttselectall.setActionCommand(ResourceManager.getInstance().getActionSelect());
		_buttselectall.addActionListener(this);
		_buttselectnone = new JButton("Select None");
		_buttselectnone.addActionListener(this);
		_buttselectnone.setActionCommand(ResourceManager.getInstance().getActionDelete());
		_aliaslist = new JList(AliasTree.getAliasList().toArray());
		_aliaslist.setSelectionModel(new ToggleSelectionModel());
		_aliaslist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		//_aliaslist.setSelectionInterval(0, _aliaslist.getModel().getSize() - 1);
		JPanel buttonpanel = new JPanel();
		buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.LINE_AXIS));
		buttonpanel.add(_buttselectall);
		buttonpanel.add(_buttselectnone);

		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.add(_txtfile);
		this.add(new JScrollPane(_aliaslist));
		this.add(buttonpanel);
		this.setshow();
	}
	
	public void setSelection(String[] aliaslist)
	{
		for(String alias : aliaslist)
		{
			for(int i = 0; i < _aliaslist.getModel().getSize(); ++i)
			{
				if(_aliaslist.getModel().getElementAt(i).toString().equals(alias))
				{
					_aliaslist.addSelectionInterval(i, i);
				}
			}
		}
	}
	
	public String[] getSelectedAlias()
	{
		String[] retval = new String[_aliaslist.getSelectedValues().length];
		for(int i = 0; i < _aliaslist.getSelectedValues().length; ++i)
		{
			retval[i] = _aliaslist.getSelectedValues()[i].toString();
		}
		return retval;
	}
	
	public void reload() throws Exception
	{
		_aliaslist = new JList(AliasTree.getAliasList().toArray());
		_aliaslist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		//_aliaslist.setSelectionInterval(0, _aliaslist.getModel().getSize() - 1);
	}
	
	public void sethide()
	{
		this.setVisible(false);
	}
	
	public void setshow()
	{
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent evt) 
	{
		String command = evt.getActionCommand();
		if(command.equals(ResourceManager.getInstance().getActionSelect()))
		{
			_aliaslist.setSelectionInterval(0, _aliaslist.getModel().getSize() - 1);
		}
		else if(command.equals(ResourceManager.getInstance().getActionDelete()))
		{
			_aliaslist.clearSelection();
		}
	}
}
